﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.List.PlugIn.Args;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.ListBillPlugin.XHX
{
    [Description("【表单插件】批次派工单审核和反审核"), HotUpdate]
    public class ListProgram : AbstractListPlugIn
    {
        string sql;
        public override void AfterBarItemClick(AfterBarItemClickEventArgs e)
        {
            base.AfterBarItemClick(e);
            var lv = this.View as IListView;
            if (lv == null)
            {
                return;
            }
            var selectedRows = lv.SelectedRowsInfo;
            if (selectedRows == null || selectedRows.Count == 0)
            {
                this.View.ShowMessage("当前没有行被选中！");
                return;
            }

            if (e.BarItemKey.Equals("tbApprove") || e.BarItemKey.Equals("tbSplitApprove"))
            {
                for (int i = 0; i < selectedRows.Count; i++)
                {
                    string sql = "";
                    int j = 0;
                    string fid = lv.SelectedRowsInfo.ElementAt(i).PrimaryKeyValue;
                    string fbillno = lv.SelectedRowsInfo.ElementAt(i).BillNo;
                    // string F_PLOT_NO = lv.SelectedRowsInfo.ElementAt(i).DataRow["F_PLOT_NO"].ToString();
                    sql = string.Format("SELECT e.fid,e.fentryid,e.fseq,a.F_PR_CODE  from TRPU_t_Cust_Entry100028 C LEFT JOIN T_PRD_MO M ON C.F_WIP_LOT=M.FBILLNO LEFT JOIN T_PRD_MOENTRY e on m.fid=e.fid LEFT JOIN T_BD_MATERIAL a ON e.fmaterialid=a.fmaterialid where c.fid in( SELECT fid from TRPU_t_Cust100012) and c.fid='{0}'  ", fid);
                    DataSet ds = DBUtils.ExecuteDataSet(this.Context, sql);
                    foreach (DataTable dt in ds.Tables)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            string F_PR_CODE = Convert.ToString(dr["F_PR_CODE"]);
                            int code = 0;
                            string yy = DateTime.Now.Year.ToString().Substring(3);
                            string mm = DateTime.Now.Month.ToString();
                            var UpdateSql = string.Format("SELECT max(code) max_code from sys_barcode_pr WHERE y='{0}' and mm='{1}' and barcode='{2}' ", yy, mm, F_PR_CODE);
                            DataSet dataSet = DBUtils.ExecuteDataSet(Context, UpdateSql);
                            string max_code = dataSet.Tables[0].Rows[0]["max_code"].ToString();
                            if (max_code.Equals(""))
                            {
                                code = 1;
                            }
                            else
                            {
                                code = Convert.ToInt32(max_code) + 1;
                            }
                            string id = Convert.ToString(dr["fid"]);
                            string fentryid = Convert.ToString(dr["fentryid"]);
                            string fseq = Convert.ToString(dr["fseq"]);
                            string product_code = F_PR_CODE + DateTime.Now.ToString("yyMMdd").Substring(1, 5) + code.ToString().PadLeft(4, '0');
                            UpdateSql = string.Format("UPDATE T_PRD_MOENTRY SET F_PLOT_NO='{0}',F_PRODUCT_CODE='{1}' WHERE FID='{2}' AND FENTRYID='{3}' AND FSEQ='{4}' ", fbillno, product_code, id, fentryid, fseq);
                            int result = DBUtils.Execute(Context, UpdateSql);
                            if (result != 1)
                            {
                                View.ShowWarnningMessage("更新计划订单派工单号失败！" + fbillno, "更新失败！");
                            }
                            UpdateSql = string.Format("INSERT sys_barcode_pr VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}') ", yy, mm, code, F_PR_CODE, id, fentryid, fbillno);
                            result = DBUtils.Execute(Context, UpdateSql);
                        }

                    }
                }
            }


        }

        public override void BeforeDoAction(BeforeDoActionEventArgs e)
        {
            base.BeforeDoAction(e);
            var lv = this.View as IListView;
            if (lv == null)
            {
                return;
            }
            var selectedRows = lv.SelectedRowsInfo;
            if (selectedRows == null || selectedRows.Count == 0)
            {
                this.View.ShowMessage("当前没有行被选中！");
                return;
            }
            if (e.Equals("tbReject"))
            {
                for (int i = 0; i < selectedRows.Count; i++)
                {
                    string fid = lv.SelectedRowsInfo.ElementAt(i).PrimaryKeyValue;
                    string fbillno = lv.SelectedRowsInfo.ElementAt(i).BillNo;
                    //string F_PLOT_NO = lv.SelectedRowsInfo.ElementAt(i).DataRow["F_PLOT_NO"].ToString();
                    sql = string.Format("SELECT * FROM T_PRD_MOENTRY_A WHERE FID IN( SELECT FID from T_PRD_MOENTRY WHERE F_PLOT_NO='{0}') AND FSTATUS='4' ", fbillno);
                    DataSet ds = DBUtils.ExecuteDataSet(this.Context, sql);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        View.ShowWarnningMessage("反审核失败，该批次派工单包含已开工的生产订单！");

                    }
                    else
                    {
                        var UpdateSql = string.Format("UPDATE T_PRD_MORPTENTRY SET F_PLOT_NO='' WHERE FID IN (SELECT FID FROM T_PRD_MO WHERE FBILLNO='{0}') ", fbillno);
                        int result = DBUtils.Execute(Context, UpdateSql);
                        if (result != 1)
                        {
                            View.ShowWarnningMessage("更新计划订单派工单号失败！" + fbillno, "更新失败！");
                        }
                    }
                    //UpdateSql = string.Format("DELETE sys_barcode_pr WHERE  LOT_NO='{0}' ", FBillNo);
                    //result = DBUtils.Execute(Context, UpdateSql);
                    //if (result != 1)
                    //{
                    //    View.ShowErrMessage("更新生产订单生产条码失败！", "更新失败！");
                    //    return;
                    //}

                }
            }
        }
        public override void BeforeDoOperation(BeforeDoOperationEventArgs e)
        {
            base.BeforeDoOperation(e);
        }
        public override void BeforeMenuClick(MenuArgs e)
        {
            base.BeforeMenuClick(e);
            var lv = this.View as IListView;
            if (lv == null)
            {
                return;
            }
            var selectedRows = lv.SelectedRowsInfo;
            if (selectedRows == null || selectedRows.Count == 0)
            {
                this.View.ShowMessage("当前没有行被选中！");
                return;
            }
            if (e.key.Equals("tbReject"))
            {
                for (int i = 0; i < selectedRows.Count; i++)
                {
                    string fid = lv.SelectedRowsInfo.ElementAt(i).PrimaryKeyValue;
                    string fbillno = lv.SelectedRowsInfo.ElementAt(i).BillNo;
                    //string F_PLOT_NO = lv.SelectedRowsInfo.ElementAt(i).DataRow["F_PLOT_NO"].ToString();
                    sql = string.Format("SELECT * FROM T_PRD_MOENTRY_A WHERE FID IN( SELECT FID from T_PRD_MOENTRY WHERE F_PLOT_NO='{0}') AND FSTATUS='4' ", fbillno);
                    DataSet ds = DBUtils.ExecuteDataSet(this.Context, sql);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        View.ShowWarnningMessage("反审核失败，该批次派工单包含已开工的生产订单！");

                    }
                    else
                    {
                        var UpdateSql = string.Format("UPDATE T_PRD_MORPTENTRY SET F_PLOT_NO='' WHERE FID IN (SELECT FID FROM T_PRD_MO WHERE FBILLNO='{0}') ", fbillno);
                        int result = DBUtils.Execute(Context, UpdateSql);
                        if (result != 1)
                        {
                            View.ShowWarnningMessage("更新计划订单派工单号失败！" + fbillno, "更新失败！");
                        }
                    }
                    //UpdateSql = string.Format("DELETE sys_barcode_pr WHERE  LOT_NO='{0}' ", FBillNo);
                    //result = DBUtils.Execute(Context, UpdateSql);
                    //if (result != 1)
                    //{
                    //    View.ShowErrMessage("更新生产订单生产条码失败！", "更新失败！");
                    //    return;
                    //}

                }
            }
        }
    }
}
